package com.newlink.building.userinfo.adapter

import android.view.View
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.newlink.building.common_base.model.bean.DeviceData
import com.newlink.building.enums.Module_Phone_DeviceType
import com.newlink.building.userinfo.R

/**
 * Created by xiaodong on 2024/12/12.
 */
class Module_UserInfo_DeviceManagementAdapter :
    BaseQuickAdapter<DeviceData, BaseViewHolder>(R.layout.item_device_management),
    OnItemChildClickListener, LoadMoreModule {

    init {
        addChildClickViewIds(R.id.iv_edit, R.id.iv_delete)
        setOnItemChildClickListener(this)
    }

    override fun convert(holder: BaseViewHolder, item: DeviceData) {

        when (item.devType) {
            Module_Phone_DeviceType.INDOOR.code -> {
                holder.setImageDrawable(
                    R.id.iv_device_type,
                    ContextCompat.getDrawable(context, R.drawable.device_indoor)
                )
            }

            Module_Phone_DeviceType.ACCESS_CONTROL.code -> {
                holder.setImageDrawable(
                    R.id.iv_device_type,
                    ContextCompat.getDrawable(context, R.drawable.device_access_control)
                )
            }
        }

        holder.setText(R.id.tv_device_name, item.devName)
        holder.setText(R.id.tv_device_mac, item.mac)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (view.id) {
            R.id.iv_edit -> onActionClickListener?.onEditClick(view, position)
            R.id.iv_delete -> onActionClickListener?.deleteClick(view, position)
        }
    }

    interface OnActionClickListener {
        fun onEditClick(view: View, position: Int)
        fun deleteClick(view: View, position: Int)
    }

    private var onActionClickListener: OnActionClickListener? = null
    fun setOnActionClickListener(listener: OnActionClickListener) {
        this.onActionClickListener = listener
    }

}